// 名称 : 通用uni-app网络请求,基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
export default {
	config: {
		baseUrl: '',
		header: {'Content-Type': 'application/x-www-form-urlencoded'},
		data: {},
		method: 'GET',
		timeout: 60000,
		sslVerify: false,
		success() {},
		fail() {},
		complete() {}
	},
	interceptor: {
		request: null,
		response: null
	},
	request(options,isJson) {
		if (!options) { options = {} }

		options.baseUrl = options.baseUrl || this.config.baseUrl
		options.dataType = options.dataType || this.config.dataType
		options.url = options.baseUrl + options.url
		options.data = options.data || {}
		options.method = options.method || this.config.method

		return new Promise((resolve, reject) => {
			let _config = null

			options.complete = (response) => {
				let statusCode = response.statusCode
				response.config = _config

				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) { response = newResponse }
				}
				// 统一的响应日志记录
				_reslog(response)

				if (statusCode === 200) { //成功
					resolve(response.data);
				} else {
					reject(response.data)
				}
			}

			if (this.interceptor.request) { this.interceptor.request(options) }

			//合并过来的Header参数
			options.header = Object.assign(this.config.header, options.header)
			if(isJson){ 
				options.header['Content-Type'] = 'application/json;charset=UTF-8'
			} else {
				options.header['Content-Type'] = 'application/x-www-form-urlencoded'
			}

			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()

			// 统一的请求日志记录
			_reqlog(_config)
			uni.request(_config);
		});
	},
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	postJson(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		options.header= {'Content-Type': 'application/json;charset=UTF-8'}
		return this.request(options,true)
	},
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	delete(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}


/**
 * 请求接口日志记录
 */
function _reqlog(req) {
	if (process.env.NODE_ENV === 'development') {
		console.log("【" + req.requestId + "】 地址：" + req.url + ' 的请求参数：')
		if (req.data) {
			console.log(req.data);
		}
	}
}

/**
 * 响应接口日志记录
 */
function _reslog(res) {
	let _statusCode = res.statusCode;
	if (process.env.NODE_ENV === 'development') {
		console.log("【" + res.config.requestId + "】 地址: " + res.config.url + " 的响应结果：")
		console.log(res.data)
	}
	//TODO 除了接口服务错误外，其他日志调接口异步写入日志数据库
	switch (_statusCode) {
		case 200:
			break;
		case 401:
			break;
		case 404:
			break;
		default:
			break;
	}
}
